package com.github.postsanf.yinian.widget;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/4/6.
 */

public class MySlidingMenu extends RelativeLayout {
    private static final String TAG = "SlidingMenu";
    public static final int SLIDING_WINDOW = 0;
    public static final int SLIDING_CONTENT = 1;
    private boolean mActionbarOverlay;
    public static final int TOUCHMODE_MARGIN = 0;
    public static final int TOUCHMODE_FULLSCREEN = 1;
    public static final int TOUCHMODE_NONE = 2;
    public static final int LEFT = 0;
    public static final int RIGHT = 1;
    public static final int LEFT_RIGHT = 2;
    private MyCustomViewAbove mViewAbove;
    private MyCustomViewBehind mViewBehind;
    private MySlidingMenu.OnOpenListener mOpenListener;
    private MySlidingMenu.OnCloseListener mCloseListener;
    private Handler mHandler;

    public MySlidingMenu(Context context) {
        this(context, (AttributeSet) null);
    }

    public MySlidingMenu(Activity activity, int slideStyle) {
        this(activity, (AttributeSet) null);
        this.attachToActivity(activity, slideStyle);
    }


    public void setOffsetFadeDegree(float f) {
        mViewBehind.setOffsetFadeDegree(f);
    }

    public MySlidingMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySlidingMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mActionbarOverlay = false;
        this.mHandler = new Handler();
        LayoutParams behindParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.mViewBehind = new MyCustomViewBehind(context);
        this.addView(this.mViewBehind, behindParams);
        LayoutParams aboveParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.mViewAbove = new MyCustomViewAbove(context);
        this.addView(this.mViewAbove, aboveParams);
        this.mViewAbove.setCustomViewBehind(this.mViewBehind);
        this.mViewBehind.setCustomViewAbove(this.mViewAbove);
        this.mViewAbove.setOnPageChangeListener(new MyCustomViewAbove.OnPageChangeListener() {
            public static final int POSITION_OPEN = 0;
            public static final int POSITION_CLOSE = 1;

            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            public void onPageSelected(int position) {
                if (position ==POSITION_OPEN && MySlidingMenu.this.mOpenListener != null) {
                    MySlidingMenu.this.mOpenListener.onOpen();
                } else if (position == POSITION_CLOSE && MySlidingMenu.this.mCloseListener != null) {
                    MySlidingMenu.this.mCloseListener.onClose();
                }

            }
        });
        TypedArray ta = context.obtainStyledAttributes(attrs, com.slidingmenu.lib.R.styleable.SlidingMenu);
        int mode = ta.getInt(com.slidingmenu.lib.R.styleable.SlidingMenu_mode, LEFT);
        this.setMode(mode);
        int viewAbove = ta.getResourceId(com.slidingmenu.lib.R.styleable.SlidingMenu_viewAbove, -1);
        if (viewAbove != -1) {
            this.setContent(viewAbove);
        } else {
            this.setContent(new FrameLayout(context));
        }

        int viewBehind = ta.getResourceId(com.slidingmenu.lib.R.styleable.SlidingMenu_viewBehind, -1);
        if (viewBehind != -1) {
            this.setMenu(viewBehind);
        } else {
            this.setMenu(new FrameLayout(context));
        }

        int touchModeAbove = ta.getInt(com.slidingmenu.lib.R.styleable.SlidingMenu_touchModeAbove, TOUCHMODE_MARGIN);
        this.setTouchModeAbove(touchModeAbove);
        int touchModeBehind = ta.getInt(com.slidingmenu.lib.R.styleable.SlidingMenu_touchModeBehind, TOUCHMODE_MARGIN);
        this.setTouchModeBehind(touchModeBehind);
        int offsetBehind = (int) ta.getDimension(com.slidingmenu.lib.R.styleable.SlidingMenu_behindOffset, -1.0F);
        int widthBehind = (int) ta.getDimension(com.slidingmenu.lib.R.styleable.SlidingMenu_behindWidth, -1.0F);
        if (offsetBehind != -1 && widthBehind != -1) {
            throw new IllegalStateException("Cannot set both behindOffset and behindWidth for a SlidingMenu");
        } else {
            if (offsetBehind != -1) {
                this.setBehindOffset(offsetBehind);
            } else if (widthBehind != -1) {
                this.setBehindWidth(widthBehind);
            } else {
                this.setBehindOffset(0);
            }

            float scrollOffsetBehind = ta.getFloat(com.slidingmenu.lib.R.styleable.SlidingMenu_behindScrollScale, 0.33F);
            this.setBehindScrollScale(scrollOffsetBehind);
            int shadowRes = ta.getResourceId(com.slidingmenu.lib.R.styleable.SlidingMenu_shadowDrawable, -1);
            if (shadowRes != -1) {
                this.setShadowDrawable(shadowRes);
            }

            int shadowWidth = (int) ta.getDimension(com.slidingmenu.lib.R.styleable.SlidingMenu_shadowWidth, 0.0F);
            this.setShadowWidth(shadowWidth);
            boolean fadeEnabled = ta.getBoolean(com.slidingmenu.lib.R.styleable.SlidingMenu_fadeEnabled, true);
            this.setFadeEnabled(fadeEnabled);
            float fadeDeg = ta.getFloat(com.slidingmenu.lib.R.styleable.SlidingMenu_fadeDegree, 0.33F);
            this.setFadeDegree(fadeDeg);
            boolean selectorEnabled = ta.getBoolean(com.slidingmenu.lib.R.styleable.SlidingMenu_selectorEnabled, false);
            this.setSelectorEnabled(selectorEnabled);
            int selectorRes = ta.getResourceId(com.slidingmenu.lib.R.styleable.SlidingMenu_selectorDrawable, -1);
            if (selectorRes != -1) {
                this.setSelectorDrawable(selectorRes);
            }

            ta.recycle();
        }
    }

    public void attachToActivity(Activity activity, int slideStyle) {
        this.attachToActivity(activity, slideStyle, false);
    }

    public void attachToActivity(Activity activity, int slideStyle, boolean actionbarOverlay) {
        if (slideStyle != 0 && slideStyle != 1) {
            throw new IllegalArgumentException("slideStyle must be either SLIDING_WINDOW or SLIDING_CONTENT");
        } else if (this.getParent() != null) {
            throw new IllegalStateException("This SlidingMenu appears to already be attached");
        } else {
            TypedArray a = activity.getTheme().obtainStyledAttributes(new int[]{android.R.attr.windowBackground});
            int background = a.getResourceId(0, 0);
            a.recycle();
            switch (slideStyle) {
                case SLIDING_WINDOW:
                    this.mActionbarOverlay = false;
                    ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
                    ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
                    decorChild.setBackgroundResource(background);
                    decor.removeView(decorChild);
                    decor.addView(this);
                    this.setContent(decorChild);
                    break;
                case SLIDING_CONTENT:
                    this.mActionbarOverlay = actionbarOverlay;
                    ViewGroup contentParent = (ViewGroup) activity.findViewById(android.R.id.content);
                    View content = contentParent.getChildAt(0);
                    contentParent.removeView(content);
                    contentParent.addView(this);
                    this.setContent(content);
                    if (content.getBackground() == null) {
                        content.setBackgroundResource(background);
                    }
            }

        }
    }

    public void setContent(int res) {
        this.setContent(LayoutInflater.from(this.getContext()).inflate(res, (ViewGroup) null));
    }

    public void setContent(View view) {
        this.mViewAbove.setContent(view);
        this.showContent();
    }

    public View getContent() {
        return this.mViewAbove.getContent();
    }

    public void setMenu(int res) {
        this.setMenu(LayoutInflater.from(this.getContext()).inflate(res, (ViewGroup) null));
    }

    public void setMenu(View v) {
        this.mViewBehind.setContent(v);
    }

    public View getMenu() {
        return this.mViewBehind.getContent();
    }

    public void setSecondaryMenu(int res) {
        this.setSecondaryMenu(LayoutInflater.from(this.getContext()).inflate(res, (ViewGroup) null));
    }

    public void setSecondaryMenu(View v) {
        this.mViewBehind.setSecondaryContent(v);
    }

    public View getSecondaryMenu() {
        return this.mViewBehind.getSecondaryContent();
    }

    public void setSlidingEnabled(boolean b) {
        this.mViewAbove.setSlidingEnabled(b);
    }

    public boolean isSlidingEnabled() {
        return this.mViewAbove.isSlidingEnabled();
    }

    public void setMode(int mode) {
        if (mode != LEFT && mode != RIGHT && mode != LEFT_RIGHT) {
            throw new IllegalStateException("SlidingMenu mode must be LEFT, RIGHT, or LEFT_RIGHT");
        } else {
            this.mViewBehind.setMode(mode);
        }
    }

    public int getMode() {
        return this.mViewBehind.getMode();
    }

    public void setStatic(boolean b) {
        if (b) {
            this.setSlidingEnabled(false);
            this.mViewAbove.setCustomViewBehind((MyCustomViewBehind) null);
            this.mViewAbove.setCurrentItem(1);
        } else {
            this.mViewAbove.setCurrentItem(1);
            this.mViewAbove.setCustomViewBehind(this.mViewBehind);
            this.setSlidingEnabled(true);
        }

    }

    public void showMenu() {
        this.showMenu(true);
    }

    public void showMenu(boolean animate) {
        this.mViewAbove.setCurrentItem(0, animate);
    }

    public void showSecondaryMenu() {
        this.showSecondaryMenu(true);
    }

    public void showSecondaryMenu(boolean animate) {
        this.mViewAbove.setCurrentItem(2, animate);
    }

    public void showContent() {
        this.showContent(true);
    }

    public void showContent(boolean animate) {
        this.mViewAbove.setCurrentItem(1, animate);
    }

    public void toggle() {
        this.toggle(true);
    }

    public void toggle(boolean animate) {
        if (this.isMenuShowing()) {
            this.showContent(animate);
        } else {
            this.showMenu(animate);
        }

    }

    public boolean isMenuShowing() {
        return this.mViewAbove.getCurrentItem() == 0 || this.mViewAbove.getCurrentItem() == 2;
    }

    public boolean isSecondaryMenuShowing() {
        return this.mViewAbove.getCurrentItem() == 2;
    }

    public int getBehindOffset() {
        return ((LayoutParams) this.mViewBehind.getLayoutParams()).rightMargin;
    }

    public void setBehindOffset(int i) {
        this.mViewBehind.setWidthOffset(i);
    }

    public void setBehindOffsetRes(int resID) {
        int i = (int) this.getContext().getResources().getDimension(resID);
        this.setBehindOffset(i);
    }

    public void setAboveOffset(int i) {
        this.mViewAbove.setAboveOffset(i);
    }

    public void setAboveOffsetRes(int resID) {
        int i = (int) this.getContext().getResources().getDimension(resID);
        this.setAboveOffset(i);
    }

    public void setBehindWidth(int i) {
        Display display = ((WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int width;
        try {
            Class e = Display.class;
            Class[] parameterTypes = new Class[]{Point.class};
            Point parameter = new Point();
            Method method = e.getMethod("getSize", parameterTypes);
            method.invoke(display, new Object[]{parameter});
            width = parameter.x;
        } catch (Exception var8) {
            width = display.getWidth();
        }
        this.setBehindOffset(width - i);
    }

    public void setBehindWidthRes(int res) {
        int i = (int) this.getContext().getResources().getDimension(res);
        this.setBehindWidth(i);
    }

    public float getBehindScrollScale() {
        return this.mViewBehind.getScrollScale();
    }

    public void setBehindScrollScale(float f) {
        if (f < 0.0F && f > 1.0F) {
            throw new IllegalStateException("ScrollScale must be between 0 and 1");
        } else {
            this.mViewBehind.setScrollScale(f);
        }
    }

    public void setBehindCanvasTransformer(MySlidingMenu.CanvasTransformer t) {
        this.mViewBehind.setCanvasTransformer(t);
    }

    public int getTouchModeAbove() {
        return this.mViewAbove.getTouchMode();
    }

    public void setTouchModeAbove(int i) {
        if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN
                && i != TOUCHMODE_NONE) {
            throw new IllegalStateException("TouchMode must be set to eitherTOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN or TOUCHMODE_NONE.");
        } else {
            this.mViewAbove.setTouchMode(i);
        }
    }

    public void setTouchModeBehind(int i) {
        if (i != TOUCHMODE_FULLSCREEN && i != TOUCHMODE_MARGIN
                && i != TOUCHMODE_NONE) {
            throw new IllegalStateException("TouchMode must be set to eitherTOUCHMODE_FULLSCREEN or TOUCHMODE_MARGIN or TOUCHMODE_NONE.");
        } else {
            this.mViewBehind.setTouchMode(i);
        }
    }

    public void setShadowDrawable(int resId) {
        this.setShadowDrawable(this.getContext().getResources().getDrawable(resId));
    }

    public void setShadowDrawable(Drawable d) {
        this.mViewBehind.setShadowDrawable(d);
    }

    public void setSecondaryShadowDrawable(int resId) {
        this.setSecondaryShadowDrawable(this.getContext().getResources().getDrawable(resId));
    }

    public void setSecondaryShadowDrawable(Drawable d) {
        this.mViewBehind.setSecondaryShadowDrawable(d);
    }

    public void setShadowWidthRes(int resId) {
        this.setShadowWidth((int) this.getResources().getDimension(resId));
    }

    public void setShadowWidth(int pixels) {
        this.mViewBehind.setShadowWidth(pixels);
    }

    public void setFadeEnabled(boolean b) {
        this.mViewBehind.setFadeEnabled(b);
    }

    public void setFadeDegree(float f) {
        this.mViewBehind.setFadeDegree(f);
    }

    public void setSelectorEnabled(boolean b) {
        this.mViewBehind.setSelectorEnabled(true);
    }

    public void setSelectedView(View v) {
        this.mViewBehind.setSelectedView(v);
    }

    public void setSelectorDrawable(int res) {
        this.mViewBehind.setSelectorBitmap(BitmapFactory.decodeResource(this.getResources(), res));
    }

    public void setSelectorBitmap(Bitmap b) {
        this.mViewBehind.setSelectorBitmap(b);
    }

    public void addIgnoredView(View v) {
        this.mViewAbove.addIgnoredView(v);
    }

    public void removeIgnoredView(View v) {
        this.mViewAbove.removeIgnoredView(v);
    }

    public void clearIgnoredViews() {
        this.mViewAbove.clearIgnoredViews();
    }

    public void setOnOpenListener(MySlidingMenu.OnOpenListener listener) {
        this.mOpenListener = listener;
    }

    public void setOnCloseListener(MySlidingMenu.OnCloseListener listener) {
        this.mCloseListener = listener;
    }

    public void setOnOpenedListener(MySlidingMenu.OnOpenedListener listener) {
        this.mViewAbove.setOnOpenedListener(listener);
    }

    public void setOnClosedListener(MySlidingMenu.OnClosedListener listener) {
        this.mViewAbove.setOnClosedListener(listener);
    }

    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        MySlidingMenu.SavedState ss = new MySlidingMenu.SavedState(superState, this.mViewAbove.getCurrentItem());
        return ss;
    }

    protected void onRestoreInstanceState(Parcelable state) {
        MySlidingMenu.SavedState ss = (MySlidingMenu.SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        this.mViewAbove.setCurrentItem(ss.getItem());
    }

    @SuppressLint({"NewApi"})
    protected boolean fitSystemWindows(Rect insets) {
        int leftPadding = insets.left;
        int rightPadding = insets.right;
        int topPadding = insets.top;
        int bottomPadding = insets.bottom;
        if (!this.mActionbarOverlay) {
            Log.v("SlidingMenu", "setting padding!");
            this.setPadding(leftPadding, topPadding, rightPadding, bottomPadding);
        }

        return true;
    }

    @TargetApi(11)
    public void manageLayers(float percentOpen) {
        if (Build.VERSION.SDK_INT >= 11) {
            boolean layer = percentOpen > 0.0F && percentOpen < 1.0F;
            final int layerType = layer ?View.LAYER_TYPE_HARDWARE : View.LAYER_TYPE_NONE;
            if (layerType != this.getContent().getLayerType()) {
                this.mHandler.post(new Runnable() {
                    public void run() {
                        Log.v("SlidingMenu", "changing layerType. hardware? " + (layerType == View.LAYER_TYPE_HARDWARE));
                        MySlidingMenu.this.getContent().setLayerType(layerType, (Paint) null);
                        MySlidingMenu.this.getMenu().setLayerType(layerType, (Paint) null);
                        if (MySlidingMenu.this.getSecondaryMenu() != null) {
                            MySlidingMenu.this.getSecondaryMenu().setLayerType(layerType, (Paint) null);
                        }

                    }
                });
            }

        }
    }

    public interface CanvasTransformer {
        void transformCanvas(Canvas var1, float var2);
    }

    public interface OnCloseListener {
        void onClose();
    }

    public interface OnClosedListener {
        void onClosed();
    }

    public interface OnOpenListener {
        void onOpen();
    }

    public interface OnOpenedListener {
        void onOpened();
    }

    public static class SavedState extends BaseSavedState {
        private final int mItem;
        public static final Parcelable.Creator<MySlidingMenu.SavedState> CREATOR = new Parcelable.Creator() {
            public MySlidingMenu.SavedState createFromParcel(Parcel in) {
                return new MySlidingMenu.SavedState(in);
            }

            public MySlidingMenu.SavedState[] newArray(int size) {
                return new MySlidingMenu.SavedState[size];
            }
        };

        public SavedState(Parcelable superState, int item) {
            super(superState);
            this.mItem = item;
        }

        private SavedState(Parcel in) {
            super(in);
            this.mItem = in.readInt();
        }

        public int getItem() {
            return this.mItem;
        }

        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeInt(this.mItem);
        }
    }
}
